//
//  ForgetViewController.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/21.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "ForgetViewController.h"
#import "LoginViewModel.h"

@interface ForgetViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView * DmImageView;//logo
@property (nonatomic, strong) UIImageView * BgcImageView;//背景
@property (nonatomic, strong) UILabel * welcomeLB;
@property (nonatomic, strong) UIImageView * phoneIV; //手机号背景
@property (nonatomic, strong) UIImageView * authCodeIV;
@property (nonatomic, strong) UIImageView * passwordIV;
@property (nonatomic, strong) UIImageView * confirmIV;
@property (nonatomic, strong) UITextField * numberPhoneTF; //输入框
@property (nonatomic, strong) UITextField * authCodeTF;
@property (nonatomic, strong) UITextField * passwordTF;
@property (nonatomic, strong) UITextField * confirmTF;
@property (nonatomic, strong) UIButton * validateCodeBTN;//获取验证码
@property (nonatomic, strong) UIButton * eyeBTN;  //眼睛
@property (nonatomic, strong) UIButton * submitBTN;// 提交

@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BGCOLOR;
    self.titleLabel.text = @"忘记密码";
    [self setUpAssembleView];
}

#pragma mark - 创建视图
- (void)setUpAssembleView{
    
    //logo
    UIImageView * dmImageView = [UIImageView new];
    self.DmImageView = dmImageView;
    [self.contentView addSubview:self.DmImageView];
    self.DmImageView.sd_layout.widthIs(GetScreenWidth*0.504).heightIs(GetScreenHeight*0.07).topSpaceToView(self.contentView,GetScreenHeight*0.063).leftSpaceToView(self.contentView,GetScreenWidth*0.248);
    self.DmImageView.image = [UIImage imageNamed:@"damao.cn"];
    
    // 背景
    UIImageView * bgcImageView = [UIImageView new];
    self.BgcImageView = bgcImageView;
    [self.contentView addSubview:self.BgcImageView];
    self.BgcImageView.sd_layout.widthIs(GetScreenWidth-44).heightIs(GetScreenHeight*0.5877).topSpaceToView(self.contentView,GetScreenHeight*0.205).leftSpaceToView(self.contentView,22);
    self.BgcImageView.image = [UIImage imageNamed:@"bg-white"];
    
    UILabel * welcomeLB = [UILabel new];
    self.welcomeLB= welcomeLB;
    [self.contentView addSubview:self.welcomeLB];
    self.welcomeLB.sd_layout.widthIs(GetScreenWidth*0.42).heightIs(25).topSpaceToView(self.contentView,GetScreenHeight*0.235).leftSpaceToView(self.contentView,GetScreenWidth*0.29);
    self.welcomeLB.text = @"欢迎您注册大猫网";
    self.welcomeLB.textColor = UIColorFromRGB(0x4a4a4a);
    self.welcomeLB.font = [UIFont systemFontOfSize:19.0f];
    
    
    
    // 手机号
    UIImageView * phoneIV = [UIImageView new];
    self.phoneIV = phoneIV;
    [self.contentView addSubview:self.phoneIV];
    self.phoneIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0374).leftSpaceToView(self.contentView,46);
    self.phoneIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * numberPhoneLB = [UILabel new];
    [self.contentView addSubview:numberPhoneLB];
    numberPhoneLB.sd_layout.widthIs(50).heightIs(20).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0524).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    numberPhoneLB.text = @"手机号:";
    numberPhoneLB.font = [UIFont systemFontOfSize:15];
    numberPhoneLB.textColor = UIColorFromRGB(0x808080);
    
    UITextField * numberPhoneTF = [UITextField new];
    self.numberPhoneTF = numberPhoneTF;
    [self.contentView addSubview:self.numberPhoneTF];
    self.numberPhoneTF.sd_layout.widthIs(GetScreenWidth*0.5).heightIs(20).topSpaceToView(self.welcomeLB,GetScreenHeight*0.0524).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.numberPhoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.numberPhoneTF.font = [UIFont systemFontOfSize:15];
    self.numberPhoneTF.placeholder = @"请输入您的手机号";
    self.numberPhoneTF.keyboardType = UIKeyboardTypeNumberPad;
    self.numberPhoneTF.textColor = UIColorFromRGB(0xb9b9b9);
    
    
    // 验证码
    UIImageView * authcodeIV = [UIImageView new];
    self.authCodeIV = authcodeIV;
    [self.contentView addSubview:self.authCodeIV];
    self.authCodeIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.phoneIV,GetScreenHeight*0.0299).leftSpaceToView(self.contentView,46);
    self.authCodeIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * validateCodeLB = [UILabel new];
    [self.contentView addSubview:validateCodeLB];
    validateCodeLB.sd_layout.widthIs(50).heightIs(20).topSpaceToView(self.phoneIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    validateCodeLB.text = @"验证码:";
    validateCodeLB.font = [UIFont systemFontOfSize:15];
    validateCodeLB.textColor = UIColorFromRGB(0x808080);
    
    UITextField * validateCodeTF = [UITextField new];
    self.authCodeTF = validateCodeTF;
    [self.contentView addSubview:self.authCodeTF];
    self.authCodeTF.sd_layout.widthIs(GetScreenWidth*0.3).heightIs(20).topSpaceToView(self.phoneIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.33);
    self.authCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.authCodeTF.font = [UIFont systemFontOfSize:15];
    self.authCodeTF.placeholder = @"请输入验证码";
    self.authCodeTF.delegate = self;
    self.authCodeTF.textColor = UIColorFromRGB(0xb9b9b9);
    
    // 获取验证码
    UIButton * validateCodeBTN =[UIButton new];
    self.validateCodeBTN = validateCodeBTN;
    [self.contentView addSubview:self.validateCodeBTN];
    self.validateCodeBTN.sd_layout.widthIs(80).heightIs(30).topSpaceToView(self.phoneIV,GetScreenHeight*0.0374).leftSpaceToView(self.contentView,GetScreenWidth*0.65);
    [self.validateCodeBTN setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.validateCodeBTN.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.validateCodeBTN setTintColor:[UIColor whiteColor]];
    [self.validateCodeBTN setBackgroundImage:[UIImage imageNamed:@"btn-hq"] forState:UIControlStateNormal];
    [self.validateCodeBTN addTarget:self action:@selector(forgetPassWordAuthCode:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //密码
    UIImageView * passwordIV = [UIImageView new];
    self.passwordIV = passwordIV;
    [self.contentView addSubview:self.passwordIV];
    self.passwordIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.authCodeIV,GetScreenHeight*0.0299).leftSpaceToView(self.contentView,46);
    self.passwordIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * passwordLB = [UILabel new];
    [self.contentView addSubview:passwordLB];
    passwordLB.sd_layout.widthIs(65).heightIs(20).topSpaceToView(self.authCodeIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    passwordLB.text = @"设置密码:";
    passwordLB.font = [UIFont systemFontOfSize:15];
    passwordLB.textColor = UIColorFromRGB(0x808080);
    
    UITextField * passwordTF = [UITextField new];
    self.passwordTF = passwordTF;
    [self.contentView addSubview:self.passwordTF];
    self.passwordTF.sd_layout.widthIs(GetScreenWidth*0.39).heightIs(20).topSpaceToView(self.authCodeIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.37);
    self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF.placeholder = @"请输入您的密码";
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.delegate = self;
    self.passwordTF.textColor = UIColorFromRGB(0xb9b9b9);
    self.passwordTF.font = [UIFont systemFontOfSize:15];
    
    // 眼睛
    UIButton * eyeBTN = [UIButton new];
    self.eyeBTN = eyeBTN;
    [self.contentView addSubview:self.eyeBTN];
    self.eyeBTN.sd_layout.widthIs(GetScreenHeight*0.0599).heightIs(GetScreenWidth*0.106).topSpaceToView(self.authCodeIV,GetScreenHeight*0.030).leftSpaceToView(self.contentView,GetScreenWidth*0.755);
    [self.eyeBTN setImage:[UIImage imageNamed:@"icon-eye1"] forState:UIControlStateNormal];
    [self.eyeBTN addTarget:self action:@selector(forgetClickEyeBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //确认密码
    UIImageView * confirmIV = [UIImageView new];
    self.confirmIV = confirmIV;
    [self.contentView addSubview:self.confirmIV];
    self.confirmIV.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.passwordIV,GetScreenHeight*0.0299).leftSpaceToView(self.contentView,46);
    self.confirmIV.image = [UIImage imageNamed:@"bg-yz"];
    
    UILabel * confirmLB = [UILabel new];
    [self.contentView addSubview:confirmLB];
    confirmLB.sd_layout.widthIs(65).heightIs(20).topSpaceToView(self.passwordIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.16);
    confirmLB.text = @"确认密码:";
    confirmLB.font = [UIFont systemFontOfSize:15];
    confirmLB.textColor = UIColorFromRGB(0x808080);
    
    UITextField * confirmTF = [UITextField new];
    self.confirmTF = confirmTF;
    [self.contentView addSubview:self.confirmTF];
    self.confirmTF.sd_layout.widthIs(GetScreenWidth*0.39).heightIs(20).topSpaceToView(self.passwordIV,GetScreenHeight*0.0445).leftSpaceToView(self.contentView,GetScreenWidth*0.37);
    self.confirmTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.confirmTF.placeholder = @"请输入您的确认密码";
    self.confirmTF.secureTextEntry = YES;
    self.confirmTF.delegate = self;
    self.confirmTF.textColor = UIColorFromRGB(0xb9b9b9);
    self.confirmTF.font = [UIFont systemFontOfSize:15];
    
    // 提交
    UIButton * submitBTN = [UIButton new];
    self.submitBTN = submitBTN;
    [self.contentView addSubview:self.submitBTN];
    self.submitBTN.sd_layout.widthIs(GetScreenWidth-92).heightIs(GetScreenHeight*0.0599).topSpaceToView(self.confirmIV,GetScreenHeight*0.0299).leftSpaceToView(self.contentView,46);
    [self.submitBTN setTitle:@"提交" forState:UIControlStateNormal];
    self.submitBTN.titleLabel.font = [UIFont systemFontOfSize:19.0f];
    [self.submitBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.submitBTN setBackgroundImage:[UIImage imageNamed:@"btn-dl"] forState:UIControlStateNormal];
    [self.submitBTN addTarget:self action:@selector(forgetPassWordSubmit:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma - mark 获取验证码
- (void)forgetPassWordAuthCode:(UIButton *)btn
{
    [self.numberPhoneTF resignFirstResponder];
    [self.authCodeTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.confirmTF resignFirstResponder];
    //获取验证码
    [LoginViewModel getAuthCodeSuccess:^{
        //验证码的发送成功 开始计时
        [DMTools startTime:btn];
        
    }];
}
#pragma - mark 是否隐藏密码
- (void)forgetClickEyeBtn:(UIButton *)btn{
    [self.numberPhoneTF resignFirstResponder];
    [self.authCodeTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.confirmTF resignFirstResponder];
    
    btn.selected = !btn.selected;
    // 改变button 的  状态图片
    
    if(btn.selected){
        //改变 密码 的 显示为明文（默认密文）
        self.passwordTF.secureTextEntry = NO;
        [btn setImage:[UIImage imageNamed:@"icon-eye2"] forState:UIControlStateNormal];
        
    }else{
        //改变 密码 的 显示为密文
        self.passwordTF.secureTextEntry = YES;
        [btn setImage:[UIImage imageNamed:@"icon-eye1"] forState:UIControlStateNormal];
    }
    
    
}
#pragma - mark 提交修改密码
- (void)forgetPassWordSubmit:(UIButton *)btn{
    [self.numberPhoneTF resignFirstResponder];
    [self.authCodeTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.confirmTF resignFirstResponder];
    
    if (![DMTools chickIphoneNumberRight:self.numberPhoneTF.text]) {//正确格式的手机号
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号"];
    }else if (![self.authCodeTF.text isEqualToString:@"123"]){
        [SVProgressHUD showInfoWithStatus:@"验证码错误"];
    }else if (self.passwordTF.text.length <6){
        [SVProgressHUD showInfoWithStatus:@"密码最少6位"];
    }else if(![self.passwordTF.text isEqualToString:self.confirmTF.text]){
        [SVProgressHUD showInfoWithStatus:@"两次密码输入不一样"];
    }else{
        NSDictionary *jsonDict = @{@"mobile":self.numberPhoneTF.text,@"auth_code":self.authCodeTF.text,
                                   @"password":self.passwordTF.text,@"create_time":[SecurityUtil timeStamp]};
        [LoginViewModel resetPassword:jsonDict success:^{
            
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.authCodeTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.confirmTF resignFirstResponder];
    return YES;
}
//点击屏幕空白处去掉键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.numberPhoneTF resignFirstResponder];
    [self.authCodeTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.confirmTF resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
